﻿using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WoodAdmin.Common.Filter;
using WoodAdmin.Models.ViewModels;
using SqlSugar;

namespace WoodAdmin.Controllers.Api
{
    [Route("api/[controller]/[action]")]
    [Authorize, ApiController, ApiExceptionHandle]
    public class BaseApiController : ControllerBase
    {
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        protected SqlSugarClient Db { get; } = DbContext.Db;

        /// <summary>
        /// 当前用户的语言
        /// </summary>
        public string CurrentUserLang => Request.Headers["Accept-Language"].FirstOrDefault() ?? "zh";

        /// <summary>
        /// 当前登陆人ID
        /// </summary>
        public string CurrentUserId => Request.HttpContext.User.Claims.FirstOrDefault(a => a.Type == "userid")?.Value;

        /// <summary>
        /// 接口返回成功
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="obj">数据</param>
        /// <returns></returns>
        [NonAction]
        public ResultViewModel JsonSuccess(string msg, object obj = null)
        {
            return new ResultViewModel { Status = ResultCode.OK, Msg = msg, Obj = obj };
        }

        /// <summary>
        /// 接口返回成功
        /// </summary>
        /// <param name="obj">数据</param>
        /// <returns></returns>
        [NonAction]
        public ResultViewModel JsonSuccess(object obj = null)
        {
            return new ResultViewModel { Status = ResultCode.OK, Obj = obj };
        }

        /// <summary>
        /// 接口返回失败
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="obj">数据</param>
        /// <returns></returns>
        [NonAction]
        public ResultViewModel JsonError(string msg, object obj = null)
        {
            return new ResultViewModel { Status = ResultCode.Error, Msg = msg, Obj = obj };
        }

        /// <summary>
        /// 接口返回失败
        /// </summary>
        /// <param name="obj">数据</param>
        /// <returns></returns>
        [NonAction]
        public ResultViewModel JsonError(object obj = null)
        {
            return new ResultViewModel { Status = ResultCode.Error, Obj = obj };
        }

    }
}